from PyQt4.QtCore import QString
from PyQt4.QtGui import QMainWindow, QDialog, QFileDialog
from ui_mainwindow import Ui_MainWindow

import pyglet
from common import *
from common.terrain import *
from common.map import Map
from common.doodads.nanmine import NandocariumMine
from common.units.nanwarrior import NanWarrior
from common.buildings.workopolis import Workopolis
from mapeditor.ui.chooser import *
from mapeditor.ui.options import *
from mapeditor.core.memanager import *

class MainWindow(QMainWindow, Ui_MainWindow):
	"""
	CGCraft MapEditor's main window.
	"""
	
	def __init__(self, parent=None):
		map = Map()
		MEManager.initialize(map)
		
		MEManager.map.description = 'Default initial editor\'s map.'
		MEManager.map.terrain = Terrain(MEManager.map, 20, 25)
		MEManager.available_units['NanWarrior'] = NanWarrior
		MEManager.available_buildings['Workopolis'] = Workopolis
		MEManager.available_doodads['Nandocarium Mine'] = NandocariumMine
		
		super(MainWindow, self).__init__(parent)
		self.setupUi(self)
		MEManager.glWidget = self.glWidget
		
	def on_actionQuit_triggered(self):
		"""
		Event called when the Quit menu entry is triggered.
		"""
		
		exit()

	def editScenarioClick(self, bool):
		"""
		Scenario button callback.
		"""
		
		if not(bool):
			if MEManager.edition_mode == MEManager.SCENARIO:
				self.editScenarioBtn.setChecked(True)
			return
		
		MEManager.set_edition_mode(MEManager.SCENARIO)
		self.editUnitsBtn.setChecked(False)
		self.editBuildingsBtn.setChecked(False)
		self.editDoodadsBtn.setChecked(False)
		
		self.toolAddBtn.setEnabled(False)
		self.selEditBtn.setEnabled(False)
		self.selDeleteBtn.setEnabled(False)
		self.selUpBtn.setEnabled(True)
		self.selDownBtn.setEnabled(True)
		self.teamSelection.setEnabled(False)
		
		self.glWidget.updateGL()
	
	def editUnitsClick(self, bool):
		"""
		Units button callback.
		"""
		
		if not(bool):
			if MEManager.edition_mode == MEManager.UNITS:
				self.editUnitsBtn.setChecked(True)
			return
		
		MEManager.set_edition_mode(MEManager.UNITS)
		self.editScenarioBtn.setChecked(False)
		self.editBuildingsBtn.setChecked(False)
		self.editDoodadsBtn.setChecked(False)
		
		self.toolAddBtn.setEnabled(True)
		self.selEditBtn.setEnabled(True)
		self.selDeleteBtn.setEnabled(True)
		self.selUpBtn.setEnabled(False)
		self.selDownBtn.setEnabled(False)
		self.teamSelection.setEnabled(True)
		
		self.glWidget.updateGL()
		
	def editBuildingsClick(self, bool):
		"""
		Buildings button callback.
		"""
		
		if not(bool):
			if MEManager.edition_mode == MEManager.BUILDINGS:
				self.editBuildingsBtn.setChecked(True)
			return
		
		MEManager.set_edition_mode(MEManager.BUILDINGS)
		self.editUnitsBtn.setChecked(False)
		self.editScenarioBtn.setChecked(False)
		self.editDoodadsBtn.setChecked(False)
		
		self.toolAddBtn.setEnabled(True)
		self.selEditBtn.setEnabled(True)
		self.selDeleteBtn.setEnabled(True)
		self.selUpBtn.setEnabled(False)
		self.selDownBtn.setEnabled(False)
		self.teamSelection.setEnabled(True)
		
		self.glWidget.updateGL()
	
	def editDoodadsClick(self, bool):
		"""
		Doodads button callback.
		"""
		
		if not(bool):
			if MEManager.edition_mode == MEManager.DOODADS:
				self.editDoodadsBtn.setChecked(True)
			return
		
		MEManager.set_edition_mode(MEManager.DOODADS)
		self.editScenarioBtn.setChecked(False)
		self.editUnitsBtn.setChecked(False)
		self.editBuildingsBtn.setChecked(False)
		
		self.toolAddBtn.setEnabled(True)
		self.selEditBtn.setEnabled(True)
		self.selDeleteBtn.setEnabled(True)
		self.selUpBtn.setEnabled(False)
		self.selDownBtn.setEnabled(False)
		self.teamSelection.setEnabled(False)
		
		self.glWidget.updateGL()
	
	def mapOptionsClick(self):
		"""
		Options button callback.
		"""
		
		dialog = OptionsDialog(self)
		result = dialog.exec_()
		
		if result == QDialog.Rejected: return
		
		MEManager.map.terrain.set_size(dialog.map_width, dialog.map_height)
		MEManager.map.description = dialog.description
		MEManager.map.ctf_win = dialog.ctf_win
		MEManager.map.destroy_all_win = dialog.destroy_all_win
		MEManager.map.horn_win = dialog.horn_win
	
	def rangeChange(self, int):
		"""
		Selection range slider value change callback.
		"""
		MEManager.range = int
		
	def selectionAddClick(self):
		"""
		Selection Add button callback.
		"""
		
		if MEManager.edition_mode == MEManager.UNITS:
			dialog = Chooser(MEManager.available_units.keys(), self)
			result = dialog.exec_()
			
			if result == QDialog.Rejected or dialog.selection is None:
				return
			
			MEManager.hovered = None
			MEManager.selected = None
			MEManager.adding = MEManager.available_units[str(dialog.selection)]
			
		elif MEManager.edition_mode == MEManager.BUILDINGS:
			dialog = Chooser(MEManager.available_buildings.keys(), self)
			result = dialog.exec_()
			
			if result == QDialog.Rejected or dialog.selection is None:
				return
			
			MEManager.hovered = None
			MEManager.selected = None
			MEManager.adding = MEManager.available_buildings[str(dialog.selection)]
			
		elif MEManager.edition_mode == MEManager.DOODADS:
			dialog = Chooser(MEManager.available_doodads.keys(), self)
			result = dialog.exec_()
			
			if result == QDialog.Rejected or dialog.selection is None:
				return
			
			MEManager.hovered = None
			MEManager.selected = None
			MEManager.adding = MEManager.available_doodads[str(dialog.selection)]
	
	def selectionEditClick(self):
		"""
		Selection Edit button callback.
		"""
		pass
	
	def selectionDeleteClick(self):
		"""
		Selection Delete button callback.
		"""
		
		if MEManager.selected is None: return
		
		if MEManager.edition_mode == MEManager.UNITS:
			MEManager.map.remove(MEManager.selected)
			MEManager.selected = None
			self.glWidget.updateGL()
			
		elif MEManager.edition_mode == MEManager.BUILDINGS:
			MEManager.map.remove(MEManager.selected)
			MEManager.selected = None
			self.glWidget.updateGL()
			
		elif MEManager.edition_mode == MEManager.DOODADS:
			MEManager.map.remove(MEManager.selected)
			MEManager.selected = None
			self.glWidget.updateGL()
	
	def selectionMoveUpClick(self):
		"""
		Selection Move Up button callback.
		"""
		
		if MEManager.selected is None: return
		
		if MEManager.edition_mode == MEManager.SCENARIO:
			for block in MEManager.selected:
				block.move(10)
			self.glWidget.updateGL()
	
	def selectionMoveDownClick(self):
		"""
		Selection Move Down button callback.
		"""
		
		if MEManager.selected is None: return
		
		if MEManager.edition_mode == MEManager.SCENARIO:
			for block in MEManager.selected:
				block.move(-10)
			self.glWidget.updateGL()
	
	def teamChanged(self, qstring):
		"""
		Team combo box changed callback.
		"""
		
		newTeam = str(qstring)
		if newTeam == 'Blue':
			MEManager.team = BLUE
		elif newTeam == 'Yellow':
			MEManager.team = YELLOW
		
	def menuNewClick(self):
		"""
		Menu New item callback.
		"""
		
		map = Map()
		Manager.map = map
		
		MEManager.map.description = 'Default initial editor\'s map.'
		MEManager.map.terrain = Terrain(MEManager.map, 20, 25)
	
	def menuOpenClick(self):
		"""
		Menu Open item callback.
		"""
		
		path = QFileDialog.getOpenFileName(self, 'Open Map', '../data/maps/',
											'CG Craft Map file (*.cgm)')
		if len(path) == 0: return
		
		if not(MEManager.map.load(path)):
			# TODO: Janela de erro
			print 'An error occurred while loading the map.'
	
	def menuSaveClick(self):
		"""
		Menu Save item callback.
		"""
		
		path = QFileDialog.getSaveFileName(self, 'Save Map', '../data/maps/',
											'CG Craft Map file (*.cgm)')
		if len(path) == 0: return
		
		if not(MEManager.map.save(path)):
			# TODO: Janela de erro
			print 'An error occurred while saving the map.'
	
	def menuQuitClick(self):
		"""
		Menu Quit item callback.
		"""
		
		self.quit()
	
	def menuHelpClick(self):
		"""
		Menu Help item callback.
		"""
		
		pass
	
	def menuAboutClick(self):
		"""
		Menu About item callback.
		"""
		
		pass
